<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:pbs="http://schema.pbs.gov.au"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:db="http://docbook.org/ns/docbook"
  version="1.0">

  <db:article>
    <db:info>
      <db:title>Extract Complex Authority Required Restriction Items</db:title>

      <db:copyright>
        <db:year>2013</db:year>
        <db:year>2011</db:year>
        <db:year>2010</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>2.2.1</db:revnumber>
          <db:date>2013-01-25</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed display of all matching private hospital item codes.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.2</db:revnumber>
          <db:date>2013-01-25</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed PBS namespace, no-contact value. Remove 'SZ' program.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1</db:revnumber>
          <db:date>2011-11-15</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Modified keys to address performance issues</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0</db:revnumber>
          <db:date>2011-02-11</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Ported to v2.4 schema.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2010-08-26</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This stylesheet extracts item codes of items that have Complex Authority Required restrictions (CARRS) from the PBS XML document.</db:para>
    <db:para>The source document is the PBS XML document, schema-valid to v2.4 (or later) of the PBS XML Schema.</db:para>
    <db:para>The result is text. Each line lists a PBS item that has a CARR. The line lists the Public Hospital item code, Private Hospital item code, drug name and form-and-strength of the item delimited by tab characters.</db:para>
  </db:article>

  <xsl:output method="text"/>
  <xsl:strip-space elements='*'/>

  <xsl:variable name='no-contact'>auth:streamlined</xsl:variable>

  <xsl:variable name='public'
    select='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "HB"]/pbs:prescribing-rule[@type = "authority-required"]'/>

  <xsl:key name='mpp'
    match='/pbs:root/pbs:drugs-list/pbs:mp/pbs:mpp'
    use='concat("#", @xml:id)'/>
  <xsl:key name='listings-by-mpp'
    match='pbs:prescribing-rule[pbs:ready-prepared]'
    use='substring(pbs:ready-prepared/pbs:mpp-reference/@xlink:href, 2)'/>

  <db:template>
    <db:para>Analyse every non-streamlined authority-required item in the Public Hospital HSD program ("TZ" is included for special authority items). For each item, find its MPP and analyse every listing for the MPP. Filter those listings to find those in the Private Hospital HSD program. Find the one with matching MQ/Rpts.</db:para>
  </db:template>
  <xsl:template match="/">
    <xsl:apply-templates select='$public[pbs:ready-prepared]'/>
  </xsl:template>

  <xsl:template match='pbs:prescribing-rule[@type = "authority-required"]'>
    <xsl:variable name="this" select="."/>
    <xsl:variable name='this.drug'
      select='key("mpp", pbs:ready-prepared/pbs:mpp-reference/@xlink:href)'/>

    <xsl:variable name='private'
      select='key("listings-by-mpp", $this.drug/@xml:id)[ancestor::pbs:listings-list/pbs:info[pbs:code = "HS"]][pbs:ready-prepared/pbs:maximum-quantity[1] = current()/pbs:ready-prepared/pbs:maximum-quantity[1] and pbs:ready-prepared/pbs:number-repeats = current()/pbs:ready-prepared/pbs:number-repeats]'/>

    <xsl:for-each select='$private'>
      <xsl:value-of select='$this/pbs:code'/>
      <xsl:text>	</xsl:text>
      <xsl:value-of select='pbs:code'/>
      <xsl:text>	</xsl:text>
      <xsl:apply-templates select='$this.drug/parent::pbs:mp/db:title'/>
      <xsl:text>	</xsl:text>
      <xsl:apply-templates select='$this.drug/db:title'/>
      <xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match='db:title'>
    <xsl:apply-templates mode='textonly'/>
  </xsl:template>

</xsl:stylesheet>
